package cate.game.role.res.guild.po.my.mall;

import cate.common.table.d.GDGuild;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.common.table.guild.GuildMallItemRow;
import easy.java.dev.note.NoteClass;
import cate.game.role.Role;
import lombok.extern.slf4j.Slf4j;

@NoteClass(value = "公会宝库处理")
@Slf4j
public class GuildMallHandler {
    /**
     * 商城道具购买
     */
    public void buyItem(Role role, int mallId, String idHex, int num) {
        GameResult<GuildMallPO> getMallRes = role.getRes().guild.mall.getMall(mallId);
        if (!getMallRes.ok()) {
            return;
        }
        GuildMallPO mall = getMallRes.data;
        GuildMallItem item = mall.getItem(idHex);
        // 检查购买资格
        GameResult<Void> r = checQualification(role, item, num);
        if (!r.ok()) {
            return;
        }
        GuildMallItemRow row = item.getRow();

        // 本次购物的总花费
        MixRes consume = new MixRes(row.consumeConfig);
        consume.multiply(num);

        // 本次购物的总奖励
        MixRes reward = new MixRes(row.itemConfig);
        reward.multiply(num);

        // 执行扣费，发放奖励
        r = basicBuy(role, consume, reward);
        if (!r.ok()) {
            return;
        }
        role.getGame().notice.dialogReward(role, reward);

        // 记录商品购买次数
        item.purchaseNum += num;

        // 玩家历史
        role.getHistory().action.guildMallBuy(num);
        if (role.getRes().guild.checkGuild().ok()) {
            role.getRes().guild.active.addActive(GDGuild.activeTask.GUILD_SHOP);
        }
        // 商店信息更新
        mall.noticeUpdate();
    }

    /**
     * 商城购买资格检查
     */
    private GameResult<Void> checQualification(Role role, GuildMallItem item, int num) {
        GameResult<Void> r = new GameResult<>();
        if (item == null) {
            role.getGame().notice.message(role, "商品不存在");
            return r.fail("商品不存在");
        }
        GuildMallItemRow row = item.getRow();
        if (row == null) {
            role.getGame().notice.message(role, "商城表配置错误");
            return r.fail("商城表配置错误");
        }
        // 检查购买次数
        if (num <= 0 || row.limitNum > 0 && item.purchaseNum > row.limitNum - num) {
            role.getGame().notice.message(role , "已售罄");
            return r.fail("已售罄");
        }
        // VIP等级验证
        if (role.getRes().guild.getGuild().basic.level < row.guildRequired) {
            role.getGame().notice.message(role, String.format("公会%d级以上才能购买", row.guildRequired));
            return r.fail("公会" + row.guildRequired + "级以上才能购买");
        }
        return r.success();
    }

    /**
     * 基本购买逻辑
     */
    public GameResult<Void> basicBuy(Role role, MixRes consume, MixRes reward) {
        GameResult<Void> r = new GameResult<>();
        if (reward == null || reward.items == null) {
            role.getGame().notice.message(role , "奖励配置错误");
            return r.fail();
        }
        // 看看钱够不够
        if (consume != null && consume.items != null) {
            GameResult<MixResItem> consumeRes = consume.consumeCheck(role, null);
            if (!consumeRes.ok()) {
                role.getGame().notice.tipResItem(role, consumeRes.data);
                return r.fail(consumeRes);
            }
        }
        // 看看格子够不够
        r = reward.addCheck(role);
        if (!r.ok()) {
            role.getGame().notice.message(role , "背包容量不足");
            return r;
        }
        // 通过验证,扣除资源,添加物品！
        if (consume != null && consume.items != null) {
            consume.consumeDirect(role, null);
        }
        reward.add(role);
        return r.success();
    }
}
